import { ElMessageBox, ElMessage } from 'element-plus'
import { useUserStoreHook } from '@/store'
import { $t } from '@/languages/index'
import { computed } from 'vue'
// 错误代码消息提示框
const tip = (message = 'Error') => {
  ElMessage.error({
    message,
    duration: 1000 * 2,
    type: 'error'
  })
}

// 错误代码捕获
export const errorHandler = status => {
  const messages = computed(() => ({
    302: $t('common.error.redirect'),
    400: $t('common.error.request_error'),
    401: $t('common.error.unauthorized'),
    403: $t('common.error.no_permission'),
    404: $t('common.error.not_found'),
    405: $t('common.error.method_not_allowed'),
    408: $t('common.error.timeout'),
    422: $t('common.error.unprocessable_entity'),
    500: $t('common.error.server_error'),
    501: $t('common.error.not_implemented'),
    502: $t('common.error.restart'),
    503: $t('common.error.unavailable'),
    504: $t('common.error.temporarily_unavailable'),
    505: $t('common.error.http_not_supported'),
    default: $t('common.error.system_error')
  }))

  tip(messages.value[status] || messages.value.default)
}

export const handleLogout = () => {
  ElMessageBox.confirm(
    'You have been logged out, you can cancel to stay on this page, or log in again',
    'Confirm logout',
    {
      confirmButtonText: 'Re-Login',
      cancelButtonText: 'Cancel',
      type: 'warning'
    }
  ).then(() => {
    // 在此处重新处理登出逻辑
    // store.dispatch('user/resetTokenAction').then(() => {
    //   location.reload()
    // })
    useUserStoreHook().logout()
    location.reload()
  })
}
